package com.cskaoyan.service.mallservice;

import com.cskaoyan.bean.mallbean.Category;
import com.cskaoyan.bean.mallbean.CategoryExample;
import com.cskaoyan.bo.mallbo.CategoryCreateBo;
import com.cskaoyan.bo.mallbo.CategoryDeleteBo;
import com.cskaoyan.bo.mallbo.CategoryUpdateBo;
import com.cskaoyan.mapper.mallmapper.CategoryMapper;
import com.cskaoyan.vo.mallvo.CategoryCreateVo;
import com.cskaoyan.vo.mallvo.CategoryListVo;
import com.cskaoyan.vo.mallvo.Categoryl1Vo;
import com.cskaoyan.vo.wx.WxCatalogCurrentVo;
import com.cskaoyan.vo.wx.WxCatalogIndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hsp
 * @date 2021/5/10 11:23
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<CategoryListVo> list() {

        //获得一级分类
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(0);
        List<Category> category1List = categoryMapper.selectByExample(categoryExample);

        List<CategoryListVo> categoryListVos = new ArrayList<>();
        for (Category category1 : category1List) {

            CategoryListVo tempCategoryListVo = new CategoryListVo(
                    category1.getId(),
                    category1.getName(),
                    category1.getKeywords(),
                    category1.getDesc(),
                    category1.getIconUrl(),
                    category1.getPicUrl(),
                    category1.getLevel(),
                    null
            );
//            获得二级分类
            CategoryExample categoryExample2 = new CategoryExample();
            categoryExample2.createCriteria().andPidEqualTo(category1.getId()).andDeletedEqualTo(0);
            List<Category> category2List = categoryMapper.selectByExample(categoryExample2);
            List<CategoryListVo.ChildrenBean> childrenBeanList = new ArrayList<>();
            for (Category category2 : category2List) {
                CategoryListVo.ChildrenBean childrenBean = new CategoryListVo.ChildrenBean(
                        category2.getId(),
                        category2.getName(),
                        category2.getKeywords(),
                        category2.getDesc(),
                        category2.getIconUrl(),
                        category2.getPicUrl(),
                        category2.getLevel()
                );
                childrenBeanList.add(childrenBean);
            }

            tempCategoryListVo.setChildren(childrenBeanList);
            categoryListVos.add(tempCategoryListVo);

        }

        return categoryListVos;
    }

    @Override
    public CategoryCreateVo createCategory(CategoryCreateBo categoryCreateBo) {

        Category category = new Category(
                null,
                categoryCreateBo.getName(),
                categoryCreateBo.getKeywords(),
                categoryCreateBo.getDesc(),
                categoryCreateBo.getPid(),
                categoryCreateBo.getIconUrl(),
                categoryCreateBo.getPicUrl(),
                categoryCreateBo.getLevel(),
                Byte.valueOf("88"),
                new Date(),
                new Date(),
                false
        );
        categoryMapper.insert(category);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CategoryCreateVo categoryCreateVo = new CategoryCreateVo(category.getId(),
                category.getName(),
                category.getKeywords(),
                category.getDesc(),
                category.getPid(),
                category.getIconUrl(),
                category.getPicUrl(),
                category.getLevel(),
                simpleDateFormat.format(category.getAddTime()),
                simpleDateFormat.format(category.getUpdateTime()));

        return categoryCreateVo;
    }

    @Override
    public List<Categoryl1Vo> l1() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(0);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        List<Categoryl1Vo> categoryl1Vos = new ArrayList<>();
        for (Category category : categoryList) {
            Categoryl1Vo tempCategoryl1Vo = new Categoryl1Vo();
            tempCategoryl1Vo.setLabel(category.getName());
            tempCategoryl1Vo.setValue(category.getId());
            categoryl1Vos.add(tempCategoryl1Vo);
        }


        return categoryl1Vos;
    }

    @Override
    public void updateCategory(CategoryUpdateBo categoryUpdateBo) {
        Category category = new Category(
                categoryUpdateBo.getId(),
                categoryUpdateBo.getName(),
                categoryUpdateBo.getKeywords(),
                categoryUpdateBo.getDesc(),
                categoryUpdateBo.getPid(),/*pid*/
                categoryUpdateBo.getIconUrl(),
                categoryUpdateBo.getPicUrl(),
                categoryUpdateBo.getLevel(),
                null,/*sortOrder*/
                null,/*addTime*/
                new Date(),/*updateTime*/
                false
        );

        categoryMapper.updateByPrimaryKeySelective(category);

    }

    @Override
    public void logicDeleteCategory(CategoryDeleteBo categoryDeleteBo) {

        categoryMapper.logicDeleteCategory(categoryDeleteBo.getId());

    }

    @Override
    public WxCatalogIndexVo index() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(0);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        int firstId = Integer.MAX_VALUE;
        for (Category category : categoryList) {
            firstId = Math.min(category.getId(),firstId);
        }

        Category currentCategory = categoryMapper.selectByPrimaryKey(firstId);
        CategoryExample currentSubCategory = new CategoryExample();
        currentSubCategory.createCriteria().andLevelEqualTo("L2").andPidEqualTo(firstId).andDeletedEqualTo(0);
        List<Category> currentSubCategoryList = categoryMapper.selectByExample(currentSubCategory);

        WxCatalogIndexVo wxCatalogIndexVo = new WxCatalogIndexVo();
        wxCatalogIndexVo.setCategoryList(categoryList);
        wxCatalogIndexVo.setCurrentSubCategory(currentSubCategoryList);
        wxCatalogIndexVo.setCurrentCategory(currentCategory);

        return wxCatalogIndexVo;
    }

    @Override
    public WxCatalogCurrentVo current(Integer id) {

        Category category = categoryMapper.selectByPrimaryKey(id);
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelEqualTo("L2").andPidEqualTo(id).andDeletedEqualTo(0);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        WxCatalogCurrentVo wxCatalogCurrentVo = new WxCatalogCurrentVo();
        wxCatalogCurrentVo.setCurrentCategory(category);
        wxCatalogCurrentVo.setCurrentSubCategory(categories);

        return wxCatalogCurrentVo;
    }
}
